package com.scunewscreeper.repository;

import com.scunewscreeper.entity.DataTNews;
import com.scunewscreeper.vo.HistoryViewVO;
import com.scunewscreeper.vo.StarNewsVO;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Component;

import javax.transaction.Transactional;

/**
 * \* Created with IntelliJ IDEA.
 * \* User: 大龙
 * \* Date: 2021/11/14
 * \* Time: 20:10
 * \* Description:
 * \
 */
public interface DataTNewsRepository extends JpaRepository<DataTNews,Integer> {

    DataTNews findBydId(String newsId);

    @Query("update DataTNews news set news.dLikeNum=news.dLikeNum+?2 where news.dId=?1")
    @Modifying
    @Transactional
    void UpdateLikeNum(String newsId,Integer by);

    @Modifying
    @Query("update DataTNews news set news.dReadNum=news.dReadNum+?2 where news.dId=?1")
    @Transactional
    void UpdateReadNum(String newsId,Integer by);

    @Query(value = "select news from DataTNews news where news.dSource=?1 order by news.dSubdate desc ")
    Page<DataTNews> findByDSourcePageable(String source, Pageable pageable);

    @Query(value = "select news from DataTNews news where news.dType=?1 order by news.dSubdate desc ")
    Page<DataTNews> findByDTypePageable(String type,Pageable pageable);

    @Query(value = "select news from DataTNews news where news.dType=?1 and news.dSource=?2 order by news.dSubdate desc ")
    Page<DataTNews> findByDTypeAndDSourcePageable(String type,String source,Pageable pageable);

    @Query(value = "select news from DataTNews news order by news.dSubdate desc ")
    Page<DataTNews> findAllByPageable(Pageable pageable);

    @Query("select new com.scunewscreeper.vo.HistoryViewVO(news.dId,news.dSource,news.dType,news.dTitle,news.dPhoto)"+
            "from DataTNews news where news.dId=?1")
    HistoryViewVO GetHistoryVoByNewsId(String newsId);

}
